import {createRouter, createWebHistory} from 'vue-router'
import {ElMessage} from "element-plus";
const Login = () => import('@/views/Login/login.vue')
const Register = () => import('@/views/Register/register.vue')
const Home = () => import('@/views/Home/home.vue')
const homepage = () => import('@/views/Home/homepage.vue')
const classManagement = () => import("@/views/Class/classManagement.vue")
const activateStu = () => import("@/views/UserManagement/activateStu.vue")
const teaInfo = () =>import('@/views/UserManagement/teaInfo.vue')
const stuInfo = () =>import('@/views/UserManagement/stuInfo.vue')
const groupManagement = () =>import('@/views/Group/groupManagement.vue')
const userDetail = () =>import('@/views/User/user_detail.vue')
const routes = [
    {
        path: '/login',
        name: 'login',
        component: Login,
    },
    {
        path: '/register',
        name: 'register',
        component: Register
    },
    {
        path: '/',
        component: Home,
        redirect: '/login',//首页默认重定向到登录页
        children: [
            {
                path: 'home',
                name: 'home',
                component: homepage,
                meta: {
                    isLoggedIn: true,
                }
            },
            {
                path: '/userManagement',
                name: 'userManagement',
                meta: {
                    isLoggedIn: true,
                    title: '用户管理'
                },
                children: [{
                    path: 'activateStu',
                    name: 'activateStu',
                    component: activateStu,
                    meta: {
                        isLoggedIn: true,
                        title: '激活学生账号'
                    },
                },{
                    path: 'teaInfo',
                    name: 'teaInfo',
                    component: teaInfo,
                    meta: {
                        isLoggedIn: true,
                        title: '所有老师信息'
                    },
                }, {
                    path: 'stuInfo',
                    name: 'stuInfo',
                    component: stuInfo,
                    meta: {
                        isLoggedIn: true,
                        title: '所有学生信息'
                    },
                }],
            },
            {
                path: '/groupManagement',
                name: 'groupManagement',
                component: groupManagement,
                meta: {
                    isLoggedIn: true,
                    title: '群组管理'
                }
            },
            {
                path: '/groupManagement',
                name: 'groupManagement',
                component: groupManagement,
                meta: {
                    isLoggedIn: true,
                    title: '群组管理'
                }
            },
            {
                path: '/classManagement',
                name: 'classManagement',
                component: classManagement,
                meta: {
                    isLoggedIn: true,
                    title: '班级管理'
                }
            },
            {
                path: '/userDetail',
                name: 'userDetail',
                component: userDetail,
                meta: {
                    isLoggedIn: true,
                    title: '个人中心'
                }
            }
        ]
    },
]
const router = createRouter({
    history: createWebHistory(),
    routes
})
router.beforeEach((to, from, next) => {
    if (to.meta.isLoggedIn) {
        try {
            let token = JSON.parse(localStorage.getItem('userInfo')).userInfo.token
            if (!token) {
                ElMessage.warning("请先登录！")
                next('/login')
            } else {
                next()
            }
        } catch (error) {
            ElMessage.warning("请先登录！")
            next('/login')
        }
    } else {
        next()
    }
})
export default router